// 设置背景图片刷新页面切换
function setbg() {
    let picindex = Math.floor(Math.random() * 30) + 1;
    $("body").css({
        "background-color": "#f5f5d5",
        "background-image": "url(./images/bgs" + "/" + picindex + ".png ) ",
        "background-repeat": "no-repeat",
        "background-size": "cover",
        "width":"100%",
        "height":"100%"
    })
}

// 设置页面字体特效（自动回滚）
function setfontRollback(motto) {
    // var types = ["麻雀虽小，五脏俱全.", "前人栽树，后人乘凉.", "千丈麻绳，终须有结.", "宝镜磨清，只照一室；心镜磨清,可照六合."];
    // motto ? types = motto : types
    var types = motto
    var words = $(".wordsbox .words");
    var stopType = false; //用于停止自动打字的
    var index = 0;
    var tempWords = "";
    var isNext = false;
    var time = 200;
    var startType = setInterval(function() {
        if (stopType) {
            //如果需要停止打字
            clearInterval(startType);
        }
        if (tempWords.length === 0) {
            //如果删完了，就开始
            if (isNext) {
                index++;
                index = index % (types.length);
                isNext = false;
            }
            tempWords = types[index].substring(0, tempWords.length + 1);
        } else if (tempWords.length === types[index].length && isNext === false) {
            //如果已经到头了，就要删去
            isNext = true;
            time = 5000;
        } else {
            //如果既没删完也没显示完
            if (isNext) {
                //如果是在减少
                tempWords = tempWords.substring(0, tempWords.length - 1);
                time = 150;
            } else {
                //如果在增多
                time = 200;
                tempWords = types[index].substring(0, tempWords.length + 1);
            }
        }
        words.html("&nbsp;" + tempWords);
    }, time);
};

var methods = {
    setbg: setbg,
    setfontRollback: setfontRollback
}
export { methods }